package com.alon.spring01;

import com.alon.spring01.utils.ViolationCronUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.spring.annotation.MapperScan;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;

@SpringBootApplication(scanBasePackages={"com.alon.spring01.common.config","com.alon.spring01"})
@MapperScan(value = {"com.alon.spring01.mapper"})
@ComponentScan(basePackages = {"com.alon.spring01.common.config","com.alon.spring01"})
@EnableAutoConfiguration(exclude={
        JpaRepositoriesAutoConfiguration.class//禁止springboot自动加载持久化bean
})
@ServletComponentScan(basePackages = "com.alon.spring01.servlet.MyServlet")
@RestController
@EnableAsync
public class Spring01Application {

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    private ScheduledFuture<?> future;

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }

    public static void main(String[] args) {
//        System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(Spring01Application.class, args);
    }

    @GetMapping("/hello")
    public String hello() {
        System.out.println("定时任务来了，哈哈哈哈，自定义cron定时任务");
        return "Hello world!";
    }

    @GetMapping("/calculationCron")
    public void calculationCron() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String cron = "*/5 * * * * ?";
        //业务计算cron表达式
        String curDate="2019-04-03 21:34:19";
        try {
            Date date=sdf.parse(curDate);
            System.out.println("转换后的日期 date"+date);
            String cron= ViolationCronUtils.getCron(date);
            System.out.println("cron 表达式：" + cron);
            startCron(cron);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    public String startCron(String cron) {
        future = threadPoolTaskScheduler.schedule(()->hello(), new CronTrigger(cron));
        System.out.println("future 结果：" + future.toString());
        System.out.println("DynamicTask.startCron()");
        return "startCron";
    }
}
